package sg.edu.nus.iss.vmcs.store;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

/**
 * @author Zaw
 * CashStoreItemIterator
 */

public class DrinkStoreItemIterator implements StoreItemIterator {
	private ArrayList<DrinksStoreItem> items = null;
	private int idx = 0;

	public DrinkStoreItemIterator(ArrayList<DrinksStoreItem> lsItems) {
		ArrayList<StoreItem> ls = new ArrayList<StoreItem>();
		ls.addAll(lsItems);
	}

	public StoreItem first() {
		return items.get(0);
	}

	public StoreItem next() {
		if (idx < items.size()) {
			idx++;
		}
		return items.get(idx-1);
	}

	public boolean hasNext(){
		return (idx<items.size());
	}

	public StoreItem current(){
		return items.get(idx);
	}

	public void sortByBrand() {
		Collections.sort(this.items, new BrandComparator());
		idx = -1;
	}

	public void sortByQuantity() {
		Collections.sort(this.items, new QuantityComparator());
		idx = -1;
	}
	
	

	class BrandComparator implements Comparator<DrinksStoreItem> {
		public int compare(DrinksStoreItem o1, DrinksStoreItem o2) {
			DrinksBrand d1 = (DrinksBrand) o1.getContent();
			DrinksBrand d2 = (DrinksBrand) o2.getContent();
			return d1.getName().compareTo(d2.getName());
		}
	}

	class PriceComparator implements Comparator<DrinksStoreItem> {
		public int compare(DrinksStoreItem o1, DrinksStoreItem o2) {
			DrinksBrand d1 = (DrinksBrand) o1.getContent();
			DrinksBrand d2 = (DrinksBrand) o2.getContent();
			int diff = d1.getPrice() - d2.getPrice();
			if (diff == 0) {
				diff = d1.getName().compareTo(d2.getName());
			}
			return diff;
		}
	}
	
	class QuantityComparator implements Comparator<DrinksStoreItem> {
		public int compare(DrinksStoreItem o1, DrinksStoreItem o2) {
			int diff = o1.getQuantity() - o2.getQuantity();
			if (diff == 0) {
				DrinksBrand d1 = (DrinksBrand) o1.getContent();
				DrinksBrand d2 = (DrinksBrand) o2.getContent();
				diff = d1.getName().compareTo(d2.getName());
			}
			return diff;
		}
	}

}
